﻿using System;
using System.Collections.Generic;

namespace ModelSerialize
{
    internal class TheadStack<T>
    {
        private List<T> list = new List<T>();

        public bool IsEmpty { get { lock (this) return list.Count == 0; } }

        internal void Push(T model)
        {
            lock (this)
            {
                list.Insert(0, model);
            }
        }

        internal bool TryPeek(out T result)
        {
            lock (this)
            {
                if (list.Count == 0)
                {
                    result = default(T);
                    return false;
                }

                result = list[list.Count - 1];
                return true;
            }
        }

        internal bool TryPop(out T result)
        {
            lock (this)
            {
                if (list.Count == 0)
                {
                    result = default(T);
                    return false;
                }

                result = list[list.Count - 1];
                list.Remove(result);
                return true;
            }
        }
    }
}